package me.tonywang.common.util;

import org.junit.Test;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.counting;
import static java.util.stream.Collectors.groupingBy;


public class EnumUtils {



    @Test
    public void testStreamAnyMatch() {
        boolean isContains =
                Arrays.stream(DictEnum.values())
                        .anyMatch(k -> k.getKey().equals("3"));
        System.out.println(isContains);
    }

    /**
     * 类似SQL的字段选择功能
     */
    @Test
    public void testStreamMap() {
        List<String> keys = Arrays.stream(DictEnum.values())
                .map(k -> k.getKey()).collect(Collectors.toList());
        System.out.println(keys);
    }

    @Test
    public void testStreamMap1() {
        List<Map> dicts = Arrays.stream(DictEnum.values())
                .map(k -> {
                    Map map = new HashMap<String, String>();
                    map.put(k.getKey(), k.getValue());
                    return map;
                }).collect(Collectors.toList());
        System.out.println(dicts);
    }

    @Test
    public void testStreamCount() {
        Long i = Arrays.stream(DictEnum.values())
                .map(k -> {
                    Map map = new HashMap<String, String>();
                    map.put(k.getKey(), k.getValue());
                    return map;
                }).count();
        System.out.println(i);
        if (i == 3)
            System.out.println(i);
    }

    /**
     * 类似WHERE功能
     */
    @Test
    public void testStreamFilter() {
        List<DictEnum> dicts = Arrays.stream(DictEnum.values()).filter(obj -> obj.getKey().equals("1"))
                .collect(Collectors.toList());
        System.out.println(dicts);
    }

    /**
     * 类似GROUPBY功能
     */
    @Test
    public void testStreamGroupBy() {
        Map<String, Long> dic = Arrays.stream(DictEnum.values()).collect(groupingBy(v -> v.getKey(), counting()));
        System.out.println(dic);

        Map<String, List<DictEnum>> o = Arrays.stream(DictEnum.values()).collect(groupingBy(v -> v.getKey()));
        System.out.println(o);

    }

    public static void main(String[] args) {
        boolean isContains =
                Arrays.stream(DictEnum.values())
                        .map(k -> k.getKey()).collect(Collectors.toList()).contains("2");
        System.out.println(isContains);

        isContains =
                Arrays.stream(DictEnum.values())
                        .anyMatch(k -> k.getKey().equals("2"));
        System.out.println(isContains);
    }


}
